import React, { PureComponent } from 'react';
import { Table } from 'antd';
// import store from '../../index';
import styles from './index.less';

const getValue = obj => Object.keys(obj).map(key => obj[key]).join(',');
export default class PateoStandardTable extends PureComponent {
  // 查询调用
  handleStandardTableChange = (pagination, filtersArg, sort) => {
    // const { dispatch } = store; // todo: dispatch
    const { fetchUrl, formValues, filterChange, parameterUrl,extraArgs={}, handleParams } = this.props;
    const filters = Object.keys(filtersArg).reduce((obj, key) => {
      const newObj = { ...obj };
      newObj[key] = getValue(filtersArg[key]);
      return newObj;
    }, {});
    const params = {
      page: pagination.current ? pagination.current : pagination.page,
      size: pagination.pageSize ? pagination.pageSize : pagination.size,
      ...formValues,
      ...filters,
      ...extraArgs,
    };
    if (sort.field) {
      params.sort = `${sort.field},${sort.order === 'descend' ? 'desc' : 'asc'}`;
    }
    if (filterChange) {
      filterChange({ ...formValues, ...filters });
    }

    let parm = params

    if (parameterUrl) {
      parm =  { url: parameterUrl, ...params }
    }

    if(handleParams){
      parm = handleParams(parm)
    }

    dispatch({
      type: fetchUrl,
      payload: parm,
    });

  };
  render() {
    return (
      <div className={styles.standardTable}>
        <Table
          onChange={this.handleStandardTableChange}
          size="middle"
          {...this.props.tableProps}
        />
      </div>
    );
  }
}
